# trace generated using paraview version 5.10.1
#import paraview
#paraview.compatibility.major = 5
#paraview.compatibility.minor = 10

#### import the simple module from the paraview
from paraview.simple import *
from DEAD.AutoDecoder.ParaviewMacro.NameInterpreter import get_n_slots
import os
import time
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()

working_dir='/home/hpc/Documents/Grain'
total_num=192

for index in range(0,total_num):
    # Get all files in the directory
    files = os.listdir(working_dir)

    # Filter files with .vts extension and get only the file names
    stl_file_names = [os.path.splitext(file)[0]
                        for file in files if file.endswith('.stl')]

    # 选择以 'index#' 开头的文件名
    input_str = [file for file in stl_file_names if file.startswith(f'{index}#')][0]

    # create a new 'STL Reader'
    target_stl = STLReader(registrationName=input_str, FileNames=[f'{working_dir}/{input_str}.stl'])
    n_slots = get_n_slots(input_str)
    theta = 360.0 / n_slots

    UpdatePipeline(time=0.0, proxy=target_stl)

    # get active view
    renderView1 = GetActiveViewOrCreate('RenderView')

    # show data in view
    target_stlDisplay = Show(target_stl, renderView1)

    # trace defaults for the display properties.
    target_stlDisplay.Representation = 'Surface'

    Hide(target_stl, renderView1)

    # create a new 'Reflect'
    reflect1 = Reflect(Input=target_stl)

    # Properties modified on reflect1
    reflect1.Plane = 'Y Min'

    # show data in view
    reflect1Display = Show(reflect1, renderView1)

    # trace defaults for the display properties.
    reflect1Display.Representation = 'Surface'

    # turn off scalar coloring
    #ColorBy(reflect1Display, None)

    # hide data in view
    #Hide(reflect1, renderView1)

    # update the view to ensure updated data information
    renderView1.Update()

    # change solid color
    reflect1Display.DiffuseColor = [1.0, 0.0, 0.0]

    transforms = []

    for i in range(1):
        # create a new 'Transform'
        if i == 0:
            transform1 = Transform(Input=reflect1)
        else:
            transform1 = Transform(Input=transforms[i-1])

        # Properties modified on transform1.Transform
        transform1.Transform.Rotate = [0.0, 0.0, theta]

        transforms.append(transform1)

    for i in range(1):
        # show data in view
        transform1Display = Show(transforms[i], renderView1)

        # trace defaults for the display properties.
        transform1Display.Representation = 'Surface'

        # turn off scalar coloring
        #ColorBy(transform1Display, None)

        transform1Display.DiffuseColor = [1.0, 0.0, 0.0]

    # 设置窗口大小
    renderView1.ViewSize = [1200, 800]

    # update the view to ensure updated data information
    renderView1.Update()

    # current camera placement for renderView1
    renderView1.CameraPosition = [-2, -2, -2]
    renderView1.CameraViewUp = [0.0, 1.0, 0.0]

    renderView1.ResetCamera()

    Interact()
    # 暂停1.0秒
    time.sleep(1.0)

    # destroy
    for i in range(1):
        Delete(transforms[i])
    del transforms

    Delete(reflect1)
    del reflect1

    Delete(target_stl)
    del target_stl